package com.strategycompass.zsx.strategycompass.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.strategycompass.zsx.strategycompass.R;
import com.strategycompass.zsx.strategycompass.bean.BaseBean;
import com.strategycompass.zsx.strategycompass.bean.BitWheelInfo;
import com.strategycompass.zsx.strategycompass.utils.BlmModelApi;
import com.strategycompass.zsx.strategycompass.utils.DeviceUtils;
import com.strategycompass.zsx.strategycompass.utils.DialogUtil;
import com.strategycompass.zsx.strategycompass.utils.ScreenUtil;
import com.strategycompass.zsx.strategycompass.utils.ToastUtils;
import com.strategycompass.zsx.strategycompass.view.BitView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity implements BlmModelApi.IHttpResult {
    @BindView(R.id.bitview1)
    BitView mBitview1;//转盘1
    @BindView(R.id.bitview2)
    BitView mBitview2;//转盘2
    @BindView(R.id.bitview3)
    BitView mBitview3;//转盘3
    @BindView(R.id.bitview4)
    BitView mBitview4;//转盘4
    @BindView(R.id.iv_pointer)
    ImageView mIvPointer;//指针
    @BindView(R.id.tv_device_id)
    TextView mTvDeviceId;//设备码文本框
    @BindView(R.id.btn_check)
    Button mBtnCheck;//注册
    @BindView(R.id.rl_strategy)
    RelativeLayout mRlStrategy;//全部的转盘
    @BindView(R.id.ll_device_id)
    LinearLayout mLlDeviceId;//设备码

    private List<BitWheelInfo> infos1 = new ArrayList<>();
    private List<BitWheelInfo> infos2 = new ArrayList<>();
    private List<BitWheelInfo> infos3 = new ArrayList<>();
    private List<BitWheelInfo> infos4 = new ArrayList<>();
    /**
     * 转盘的文字
     */
    private String[] mStrs1 = new String[]{"高利润", "低风险", "轻资产", "快现金", "易操作"};
    private String[] mStrs2 = new String[]{"代理加盟", "会销", "服务", "传播", "可感知价值", "命名", "鱼饵",
            "见证", "转介绍", "重复购买"};
    private String[] mStrs3 = new String[]{"城市合伙人", "平台模式", "收费站", "轻资产", "协议控制", "店中店"};
    private String[] mStrs4 = new String[]{"重资产众筹", "投行模式", "融资路演", "立体股权", "第三方增值", "杠杆并购", "零成本参股", "产业链重组",
            "产和共赢", "产品金融化", "充值分期", "联合创始人"};
    private RelativeLayout.LayoutParams mParams1;
    private RelativeLayout.LayoutParams mParams2;
    private RelativeLayout.LayoutParams mParams3;
    private RelativeLayout.LayoutParams mParams4;

    private final String URL_CHECK = "http://120.25.215.51/blmluopanapi/?m=check&act=index";
    private Map<String, String> params;
    private String mCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        getBitWheelInfos();
        setLayoutParams();
        mBitview1.setBitInfos(infos1);
        mBitview2.setBitInfos(infos2);
        mBitview3.setBitInfos(infos3);
        mBitview4.setBitInfos(infos4);
        mCode = DeviceUtils.getUniqueId(this);
        checkDeviceId(mCode);
    }

    /**
     * 联网检测设备码是否注册过
     */
    private void checkDeviceId(String code) {
        params = new HashMap<>();
        params.put("code", code);
        ProgressDialog progressDialog = DialogUtil.getProgressDialog(this, "查询中...");
        BlmModelApi blmModelApi = new BlmModelApi(this, progressDialog);
        blmModelApi.getApi(URL_CHECK, params, this);
    }

    /**
     * 动态设置偏移量
     */
    private void setLayoutParams() {
        mParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        mParams1.setMargins(ScreenUtil.getScreenWidthPix(this) / 3, ScreenUtil.getScreenWidthPix(this) / 3, ScreenUtil
                .getScreenWidthPix(this) / 3, ScreenUtil.getScreenWidthPix(this) / 3);
        mParams1.addRule(RelativeLayout.CENTER_IN_PARENT);
        mParams2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        mParams2.setMargins(ScreenUtil.getScreenWidthPix(this) / 4, ScreenUtil.getScreenWidthPix(this) / 4, ScreenUtil
                .getScreenWidthPix(this) / 4, ScreenUtil.getScreenWidthPix(this) / 4);
        mParams2.addRule(RelativeLayout.CENTER_IN_PARENT);
        mParams3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        mParams3.setMargins(ScreenUtil.getScreenWidthPix(this) / 6, ScreenUtil.getScreenWidthPix(this) / 6, ScreenUtil
                .getScreenWidthPix(this) / 6, ScreenUtil.getScreenWidthPix(this) / 6);
        mParams3.addRule(RelativeLayout.CENTER_IN_PARENT);
        mParams4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        mParams4.setMargins(ScreenUtil.getScreenWidthPix(this) / 15, ScreenUtil.getScreenWidthPix(this) / 15, ScreenUtil
                .getScreenWidthPix(this) / 15, ScreenUtil.getScreenWidthPix(this) / 15);
        mParams4.addRule(RelativeLayout.CENTER_IN_PARENT);
        mBitview1.setLayoutParams(mParams1);
        mBitview2.setLayoutParams(mParams2);
        mBitview3.setLayoutParams(mParams3);
        mBitview4.setLayoutParams(mParams4);
        mIvPointer.setLayoutParams(mParams3);
    }

    /**
     * 文字添加--动态绘制
     */
    public void getBitWheelInfos() {
        for (int i = 0; i < mStrs1.length; i++) {
            infos1.add(new BitWheelInfo(mStrs1[i], null));
        }
        for (int i = 0; i < mStrs2.length; i++) {
            infos2.add(new BitWheelInfo(mStrs2[i], null));
        }
        for (int i = 0; i < mStrs3.length; i++) {
            infos3.add(new BitWheelInfo(mStrs3[i], null));
        }
        for (int i = 0; i < mStrs4.length; i++) {
            infos4.add(new BitWheelInfo(mStrs4[i], null));
        }
    }

    @Override
    public void onSuccess(Object apidata) {
        BaseBean baseBean = (BaseBean) apidata;
        ToastUtils.showShortToast(this, baseBean.getMessage());
        if (baseBean.getCode() == 0) {
            mRlStrategy.setVisibility(View.VISIBLE);
            mLlDeviceId.setVisibility(View.GONE);
        } else {
            mRlStrategy.setVisibility(View.GONE);
            mLlDeviceId.setVisibility(View.VISIBLE);
            mTvDeviceId.setText(mCode);
        }
    }

    @OnClick({R.id.btn_check})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_check:
                checkDeviceId(mCode);
                break;
        }
    }
}
